/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package technotheist.imgcog.filter;

/**
 *
 * @author Mitch
 */
public class FilterUtil {

    private FilterUtil() {
    }
    
    
    
    protected static int getGaussSideLen(float sd) {
        int len = (int) Math.ceil(6 * sd);
        if(len % 2 == 0) {
            len += 1;
        }
        return len;
    }

    protected static float[] createGaussianConvolveKernel(float sd) {
        int sideLen = getGaussSideLen(sd);
        if(sideLen % 2 == 0) {
            sideLen += 1;
        }
        float[] kernel = new float[sideLen * sideLen];
        double a = 1.0/(2.0 * Math.PI * sd * sd);
        double b = 1.0/(2.0 * sd * sd);
        //
        int d = (sideLen - 1)/2;
        for (int x = 0; x < sideLen; x++) {
            for (int y = 0; y < sideLen; y++) {
                float dSq = (x - d) * (x - d) + (y - d) * (y - d);
                int i = y + x * sideLen;
                kernel[i] = (float) (a * Math.exp(-dSq * b));
            }
        }
        return kernel;
    }
}
